package com.controller;

import java.util.List;
import java.util.Map;
import java.io.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.dao.LogDao;
import com.dao.archiveDao;
import com.pojo.archive;
import com.pojo.file;
import com.pojo.log;
import com.service.archiveService;
import com.service.fileService;
import com.service.logService;
import com.util.MybatisSessionFactory;

@Controller
public class archiveController {
    archiveService cs=new archiveService();
    logService cz =new logService();
    fileService zz=new fileService();
	
	@RequestMapping(value = "/add.action", method = RequestMethod.POST)
	@ResponseBody
	public String add(
			@RequestParam(value = "archive_id", defaultValue = "") String archive_id,
			@RequestParam(value = "identity", defaultValue = "") String identity,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "gender", defaultValue = "") String gender,
			@RequestParam(value = "birthday", defaultValue = "") String birthday,
			@RequestParam(value = "date_retire1", defaultValue = "") String date_retire1,
			@RequestParam(value = "date_retire2", defaultValue = "") String date_retire2,
			@RequestParam(value = "sp", defaultValue = "") String sp,
			@RequestParam(value = "fc", defaultValue = "") String fc,
			@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "seniority", defaultValue = "") String seniority,
			@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response
			) {
		 List<archive> ar=cs.queryall(archive_id, identity);
		    if(0!=ar.size()){
		    	 return "REE";
		    }
		cs.add(archive_id, identity, name, gender, birthday, date_retire1, date_retire2, sp, fc, title, seniority);
		cz.add2(resultId, "添加");
		return "ok";
	}
	
	@RequestMapping(value = "/query_c.action", method = RequestMethod.POST)
	@ResponseBody
	public List<archive> query_c(
			@RequestParam(value = "archive_id", defaultValue = "") String archive_id,
			@RequestParam(value = "identity", defaultValue = "") String identity,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "gender", defaultValue = "") String gender,
			@RequestParam(value = "birthday", defaultValue = "") String birthday,
			@RequestParam(value = "date_retire1", defaultValue = "") String date_retire1,
			@RequestParam(value = "date_retire2", defaultValue = "") String date_retire2,
			@RequestParam(value = "sp", defaultValue = "") String sp,
			@RequestParam(value = "fc", defaultValue = "") String fc,
			@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "seniority", defaultValue = "") String seniority,
			@CookieValue(value = "result", defaultValue = "") String resultId,
			HttpServletResponse response) {
      //  System.out.println(archive_id);
		List<archive> result=cs.query_c(archive_id, identity, name, gender, birthday, date_retire1, date_retire2, sp, fc, title, seniority);
	//	System.out.println(result);
		return result;
		
	
	}
	@RequestMapping(value = "/queryone.action", method = RequestMethod.GET)
	@ResponseBody
	public archive queryone(
			@RequestParam(value = "id", defaultValue = "") String id,
			HttpServletResponse response) {
            archive result = cs.queryone(id);
		System.out.println(result);
		return result;
	}
	@RequestMapping(value = "/querydel.action", method = RequestMethod.POST)
	@ResponseBody
	public String querydel(
			@RequestParam(value = "archive_id", defaultValue = "") String archive_id,
			@RequestParam(value = "identity", defaultValue = "") String identity,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "gender", defaultValue = "") String gender,
			@RequestParam(value = "birthday", defaultValue = "") String birthday,
			@RequestParam(value = "date_retire1", defaultValue = "") String date_retire1,
			@RequestParam(value = "date_retire2", defaultValue = "") String date_retire2,
			@RequestParam(value = "sp", defaultValue = "") String sp,
			@RequestParam(value = "fc", defaultValue = "") String fc,
			@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "seniority", defaultValue = "") String seniority,
			HttpServletResponse response) {
		
		cs.querydel(archive_id);
	//	System.out.println(archive_id);
		cz.add2(archive_id, "删除" );
		
		return "OK";
	}
	@RequestMapping(value = "/query_update.action", method = RequestMethod.POST)
	@ResponseBody
	public String query_update(
			@RequestParam(value = "id", defaultValue = "") String id,
			@RequestParam(value = "archive_id", defaultValue = "") String archive_id,
			@RequestParam(value = "identity", defaultValue = "") String identity,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "gender", defaultValue = "") String gender,
			@RequestParam(value = "birthday", defaultValue = "") String birthday,
			@RequestParam(value = "date_retire1", defaultValue = "") String date_retire1,
			@RequestParam(value = "date_retire2", defaultValue = "") String date_retire2,
			@RequestParam(value = "sp", defaultValue = "") String sp,
			@RequestParam(value = "fc", defaultValue = "") String fc,
			@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "seniority", defaultValue = "") String seniority,
			HttpServletResponse response) {
		
		cs.query_update(id,archive_id, identity, name, gender, birthday, date_retire1, date_retire2, sp, fc, title, seniority);
	//	System.out.println(archive_id);
		cz.add2(archive_id, "修改" );
		
		return "OK";
	}
	@RequestMapping(value = "query_one.action", method = RequestMethod.POST)
	@ResponseBody
	public String query_one(
			@RequestParam(value = "search", defaultValue = "") String search,
			HttpServletResponse response
	    ){
		List<archive> map=cs.query_one(search);
		if(0==map.size()){
			return "REE";			
		}else if(1<map.size()){
			return "REE2";		
		}
		cz.add2(search, "查看" );
		System.out.println(map);
		return map.get(0).getId();
	}
	@RequestMapping(value = "/{id}/upload.action", method = RequestMethod.POST)
    @ResponseBody
    public String upload(
        @PathVariable String id,
        @RequestParam("file") MultipartFile file,
        HttpServletRequest req
    ) {
        if (file.isEmpty()) {
            System.out.println("未选择文件");
            return "ERROR_NO_FILE";
        }
        try {
            byte[] bytes = file.getBytes();
            String fileName = file.getOriginalFilename();
            String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);
            String newFileName = String.valueOf(System.currentTimeMillis()) + "." + fileExtension;
            SqlSession session=MybatisSessionFactory.getSqlSession();
            archiveDao dao = session.getMapper(archiveDao.class);
           archive a = dao.queryone(id);
            String filePath = String.format("%s\\%s\\%s\\%s",
                req.getSession().getServletContext().getRealPath("/"),
              //  GVars.UPLOAD_FILE_PATH,		//常量大写

                "upload",
                a.getId(),
                //a.get(0).getId().toString(),
                newFileName);
            File existsFile = new File(filePath);
            if (!existsFile.getParentFile().exists()) {
                if (!existsFile.getParentFile().mkdirs()) {
                    System.out.println("创建目录失败");
                    return "ERROR_ON_MKDIR";
                }
            }
            if (existsFile.exists()) {
                if (existsFile.delete()) {
                  // System.out.println("删除原始文件：" + filePath);
                } else {
                    System.out.println("删除文件失败：" + filePath);
                }
            }
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(
            new File(filePath)));
            stream.write(bytes);
            stream.close();
           zz.add3(id, newFileName);
            session.commit();
            return "OK";
        } catch (Exception e) {
            e.printStackTrace();
            return "ERROR";
        }
    }
	
	@RequestMapping(value = "/query_photo.action", method = RequestMethod.GET)
	@ResponseBody
	public List<file> query_photo(
			@RequestParam(value = "id", defaultValue = "") String id,
			HttpServletResponse response) {
            List<file> result = zz.query_photo(id);
		//System.out.println(result);
            cz.add2(id, "上传图片" );
		return result;
	}
	@RequestMapping(value = "/query_del.action", method = RequestMethod.POST)
	@ResponseBody
	public String query_del(
			@RequestParam(value = "id", defaultValue = "") String id,
			HttpServletResponse response) {
		System.out.println(id);
            zz.query_del(id);
            
            return "OK";
		
	}
	@RequestMapping(value = "/query_oone.action", method = RequestMethod.GET)
	@ResponseBody
	public List<file> query_oone(
			@RequestParam(value = "id", defaultValue = "") String id,
			HttpServletResponse response) {
		
		List<file> result = zz.query_oone(id);
		//System.out.println(result);
		return result;
	}
	@RequestMapping(value = "/query_add.action", method = RequestMethod.GET)
	@ResponseBody
	public List<archive> query_add(
			@RequestParam(value = "id", defaultValue = "") String id,
			@RequestParam(value = "archive_id", defaultValue = "") String archive_id,
			@RequestParam(value = "identity", defaultValue = "") String identity,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "gender", defaultValue = "") String gender,
			@RequestParam(value = "birthday", defaultValue = "") String birthday,
			@RequestParam(value = "date_retire1", defaultValue = "") String date_retire1,
			@RequestParam(value = "date_retire2", defaultValue = "") String date_retire2,
			@RequestParam(value = "sp", defaultValue = "") String sp,
			@RequestParam(value = "fc", defaultValue = "") String fc,
			@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "seniority", defaultValue = "") String seniority,
			HttpServletResponse response) {
		
		List<archive> result = cs.query_add(id, archive_id, identity, name, gender, birthday, date_retire1, date_retire2, sp, fc, title, seniority);
		//System.out.println(result);
		return result;
	}
	@RequestMapping(value = "/query_see.action", method = RequestMethod.GET)
	@ResponseBody
	public List<archive> query_see(
			@RequestParam(value = "id", defaultValue = "") String id,
			@RequestParam(value = "archive_id", defaultValue = "") String archive_id,
			@RequestParam(value = "identity", defaultValue = "") String identity,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "gender", defaultValue = "") String gender,
			@RequestParam(value = "birthday", defaultValue = "") String birthday,
			@RequestParam(value = "date_retire1", defaultValue = "") String date_retire1,
			@RequestParam(value = "date_retire2", defaultValue = "") String date_retire2,
			@RequestParam(value = "sp", defaultValue = "") String sp,
			@RequestParam(value = "fc", defaultValue = "") String fc,
			@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "seniority", defaultValue = "") String seniority,
			HttpServletResponse response) {
		
		List<archive> result = cs.query_see(id, archive_id, identity, name, gender, birthday, date_retire1, date_retire2, sp, fc, title, seniority);
		//System.out.println(result);
		return result;
	
	
}
	@RequestMapping(value = "/query_look.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map> query_look(
			@RequestParam(value = "date1", defaultValue = "") String date1,
			@RequestParam(value = "date2", defaultValue = "") String date2
			) {
		List<Map> result = cs.query_look(date1, date2);
		System.out.println(result);
		return result;	
}
	//删除数据库里和  TOMCAT上的图片
//	@RequestMapping(value = "/deltp.action", method = RequestMethod.POST)
//	@ResponseBody
//	  public String deltp(@RequestParam(value="id",defaultValue="")String id,
//			  HttpServletRequest req
//			  ){
//		      List<Filetp> list = service1.queryonedeltp(id);
//		      System.out.println( list.get(0).getArchive_id());  // archive的archive_id的值
//		      System.out.println(list.get(0).getName());    //file表的图片的名字 
//		      
//		 String filePath = String.format("%s\\%s\\%s\\%s",
//					req.getSession().getServletContext().getRealPath("/"),
//			        "upload",
//			        list.get(0).getArchive_id(),
//			        list.get(0).getName());
//		 File existsFile = new File(filePath);
//		 System.out.println(filePath);
//		  existsFile.delete();
//		  if (existsFile.exists()) {
//	     	 if (existsFile.delete()){System.out.println("删除原始文件："+filePath);
//	     	 }else{System.out.println("删除文件失败："+filePath);}
//	      }
//		     service1.deltp(id);
//		     System.out.println("删除");
//		   return "OK";


		
		
}


